//
//  AppDelegate.m
//  liulanqi
//
//  Created by HappyMilk 02 on 2017/1/7.
//  Copyright © 2017年 HappyMilk 02. All rights reserved.
//

#import "AppDelegate.h"
#import "IQKeyboardManager.h"

#import "WeChatQQStaticLibHeader.h"

#import "Reachability.h"


@interface AppDelegate ()<UIAlertViewDelegate>

@property (nonatomic, strong) Reachability *conn;

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    [WeChatQQApi initSdk];
    
    [[IQKeyboardManager sharedManager] setEnable:YES];
    [[IQKeyboardManager sharedManager] setEnableAutoToolbar:YES];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(networkStateChange) name:kReachabilityChangedNotification object:nil];
    self.conn = [Reachability reachabilityForInternetConnection];
    [self.conn startNotifier];
    
    [self checkNetworkState];
    
    return YES;
}
- (void)checkNetworkState{
    
    self.conn = [Reachability reachabilityForInternetConnection];
    if ([self.conn currentReachabilityStatus] == NotReachable) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"暂无网络" message:@"请前往设置" delegate:self cancelButtonTitle:@"前往" otherButtonTitles:nil, nil];
        [alert show];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"youwangluo" object:nil];
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0){
    if(buttonIndex == 0){
        NSURL * url = [NSURL URLWithString:@"prefs:root"];
        
        if([[UIApplication sharedApplication] canOpenURL:url]) {
            
            NSURL*url =[NSURL URLWithString:UIApplicationOpenSettingsURLString];           [[UIApplication sharedApplication] openURL:url];
            
        }
    }
}

- (void)networkStateChange{
    [self checkNetworkState];
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    self.backgroundTaskIdentifier =[application beginBackgroundTaskWithExpirationHandler:^(void) {
        dispatch_async(dispatch_get_main_queue(), ^{
            [application endBackgroundTask:self.backgroundTaskIdentifier];
            self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
        });
    }];
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    [application endBackgroundTask:self.backgroundTaskIdentifier];
    self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
